package com.thkj.modules.file.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.thkj.common.util.PropertiesUtils;
import com.thkj.common.util.Util;

import dp.web.BaseAction;

@RestController
@RequestMapping("/backstage/file")
public class FileAction extends BaseAction {

	@RequestMapping("/imgUpload")
	public void imgUpload(MultipartFile file, String saveType, HttpServletRequest request) {
		JSONObject json = new JSONObject();
		Long time = System.currentTimeMillis();
		String savepath = PropertiesUtils.getProperty("sys.constants.imges.savepath");
		BufferedOutputStream out;
		String fileUrl = null;
		try {
			File first = new File(savepath + File.separator + saveType + File.separator);
			if (!first.exists()) {
				first.mkdirs();
			}
			String picSuffix = Util.getPicSuffix(file.getOriginalFilename());
			File updateFile = new File(savepath + "/" + saveType + "/" + time + "." + picSuffix);
			updateFile.setExecutable(true);
			out = new BufferedOutputStream(new FileOutputStream(updateFile));
			out.write(file.getBytes());
			out.flush();
			out.close();
			fileUrl = saveType + "/" + time + "." + picSuffix;

			json.put("path",
					PropertiesUtils.getProperty("sys.constants.imges.baseurl") + "/" + saveType + "/" + fileUrl);
			json.put("url", fileUrl);
			json.put("success", true);
		} catch (IOException e) {
			e.printStackTrace();
			json.put("msg", "图片上传错误!");
			json.put("success", false);
		}
		this.outputString(json.toJSONString());
	}

	@RequestMapping("/fileUpload")
	public void fileUpload(MultipartFile file, String saveType, HttpServletRequest request) {
		JSONObject json = new JSONObject();
		Long time = System.currentTimeMillis();
		String savepath = PropertiesUtils.getProperty("sys.constants.imges.savepath");
		BufferedOutputStream out;
		String fileUrl = null;
		try {
			File first = new File(savepath + File.separator + saveType + File.separator);
			if (!first.exists()) {
				first.mkdirs();
			}
			String picSuffix = Util.getPicSuffix(file.getOriginalFilename());
			File updateFile = new File(savepath + "/" + saveType + "/" + time + "." + picSuffix);
			updateFile.setExecutable(true);
			out = new BufferedOutputStream(new FileOutputStream(updateFile));
			out.write(file.getBytes());
			out.flush();
			out.close();
			fileUrl = saveType + "/" + time + "." + picSuffix;

			json.put("path",
					PropertiesUtils.getProperty("sys.constants.imges.baseurl") + "/" + saveType + "/" + fileUrl);
			json.put("url", fileUrl);
			json.put("success", true);
		} catch (IOException e) {
			e.printStackTrace();
			json.put("msg", "文件上传错误!");
			json.put("success", false);
		}
		this.outputString(json.toJSONString());
	}

	@RequestMapping("/uploadJson")
	public void uploadJson(@RequestParam(value = "file", required = false) MultipartFile file, String saveType,
			HttpServletRequest request) {
		JSONObject json = new JSONObject();
		try {
			Long time = System.currentTimeMillis();
			String basePath = PropertiesUtils.getProperty("sys.constants.imges.savepath");
			if (!basePath.endsWith(File.separator)) {
				basePath += File.separator;
			}
			File first = new File(basePath + saveType + File.separator);
			if (!first.exists()) {
				first.mkdirs();
			}
			String filePath = basePath + saveType + File.separator + time + ".jpg";
			ImageIO.write(ImageIO.read(file.getInputStream()), "JPEG", new File(filePath));
			// 同步上传到oss
			// String endpoint = PropertiesUtils.getProperty("OSS_URL"); // 外网地址
			// String accessKeyId = PropertiesUtils.getProperty("OSS_ACCESSKEYID");
			// String accessKeySecret = PropertiesUtils.getProperty("OSS_ACCESSKEYSECRET");
			// String bucketName = PropertiesUtils.getProperty("OSS_BUCKETNAME");
			String key = saveType + "/" + time + ".jpg";
			// 创建OSSClient实例
			// OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			// 上传文件流
			// ossClient.putObject(bucketName, key, file.getInputStream());
			// 关闭client
			// ossClient.shutdown();
			json.put("error", 0);
			json.put("url", PropertiesUtils.getProperty("sys.constants.imges.baseurl") + "/" + key);
		} catch (IOException e) {
			e.printStackTrace();
			json.put("error", 1);
			json.put("message", "图片上传错误!");
		}
		this.outputString(json.toJSONString());
	}

}
